/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package csci6450.chickenFriedFS.metaServer.threads;

import csci6450.chickenFriedFS.metaServer.MetaServerLog;
import csci6450.chickenFriedFS.metaServer.NuggetServerInfoLog;
import csci6450.chickenFriedFS.nuggetCore.NuggetConstants;
import csci6450.chickenFriedFS.nuggetCore.NuggetServerInfo;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Date;
import javax.swing.Timer;

/**
 *
 * @author jacobschoen
 */
public class HeartBeatActionListener implements ActionListener {

    private NuggetServerInfo server;

    Timer timer;

    public HeartBeatActionListener(NuggetServerInfo info) {
        super();
        this.server = info;
    }

    public void setTimer(Timer set){
        this.timer = set;
    }

    public void actionPerformed(ActionEvent e) {
        String message = null;
        Socket socket = null;
        try {
            // open a socket connection
            socket = new Socket(this.server.getIpAddress(), this.server.getMetaPort());
            // open I/O streams for objects
            ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
            ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream());
            outputStream.writeObject(new Date());
            outputStream.flush();

            NuggetServerInfo updates = (NuggetServerInfo) inputStream.readObject();
            if (updates != null) {
                NuggetServerInfoLog.getInstance().updateNuggetServerInfo(updates.getIpAddress(), updates.getLastAccessed(), true, updates.getLeftOverNuggets());
                MetaServerLog.getInstance().addNuggets(updates.getNuggets());
            }
        } catch (ClassNotFoundException ex) {
            message = "Server " + this.server.getIpAddress() + ":" + this.server.getMetaPort()+" is DOWN!!";
            NuggetServerInfoLog.getInstance().updateNuggetServerInfo(this.server.getIpAddress(), new Date(), false, 0);
            this.stopTimer();
        } catch (UnknownHostException ex) {
            message = "Server " + this.server.getIpAddress() + ":" + this.server.getMetaPort()+" is DOWN!!";
            NuggetServerInfoLog.getInstance().updateNuggetServerInfo(this.server.getIpAddress(), new Date(), false, 0);
            this.stopTimer();
        } catch (IOException ex) {
            message = "Server " + this.server.getIpAddress() + ":" + this.server.getMetaPort()+" is DOWN!!";
            NuggetServerInfoLog.getInstance().updateNuggetServerInfo(this.server.getIpAddress(), new Date(), false, 0);
            this.stopTimer();
        } catch (Exception ex) {
            message = "Server " + this.server.getIpAddress() + ":" + this.server.getMetaPort()+" is DOWN!!";
            NuggetServerInfoLog.getInstance().updateNuggetServerInfo(this.server.getIpAddress(), new Date(), false, 0);
            this.stopTimer();
        } finally {
            if(socket != null){
                try {
                    socket.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }
        if (message != null){
            System.out.println(message);
        }
    }

    private void stopTimer(){
        if(this.timer != null){
            this.timer.stop();
        }
    }
}
